window.onload = function() {
    var vm = new Vue({
      el: "#view",
      data: {
        type:null,//1-楼盘-热门楼盘 2-社区-热门品牌 3 楼盘房源
        hotList:[],
        historyList:[],
        keywords:''
      },
      created: function() {
        this.type = this.getParams("type");
        this.getHistoryList();
        if(this.type == 1 || this.type == 3){
            this.getHotBuilding();
        }else if(this.type == 2||this.type == 4){
            this.getHotBrand();
        }
      },
      methods: {
        goBack(){
            window.history.go(-1);
        },
        getHotBuilding(){
            var $this = this;
            house_server.getHotBuilding({}, function(res) {
                if (res.code == 200) {
                      $this.hotList = res.data;
                }
            });
        },  
        getHotBrand(){
            var $this = this;
            house_server.hotBrand({}, function(res) {
                if (res.code == 200) {
                  $this.hotList = res.data;
                }
            });
        },      
        goToDetail(param){
          if('brandId' in param){
            window.location.href = "search-result-brand.html?brandId=" + param.brandId;
          }
          if('buildingId' in param){
            window.location.href=("search-result-build.html?keyword=" + param.buildingName);
          }
        },
        search(){
          if(this.keywords){
            this.setHistoryList(this.keywords);
          } 
          if(this.type == 1){  
            window.location.href = "search-result-build.html?keyword=" + this.keywords;                       
          }else if(this.type == 2){
            window.location.href = "search-result-community.html?keyword=" + this.keywords;
          }else if(this.type == 3){
            window.location.href = "search-result-house.html?keyword=" + this.keywords;
          }else if(this.type == 4){
            window.location.href = "search-result-brand.html?keyword=" + this.keywords;
          }
        }, 
        goToHistorySearch(item){
          this.keywords = item.value;
          this.type = item.type;
          this.search();
        }, 
        clearHistory(){
          localStorage.removeItem('searchList');
          this.getHistoryList();
        },
        //localstorage取
        getHistoryList(){
          this.historyList = localStorage.getItem('searchList') ? JSON.parse(localStorage.getItem('searchList')) : [];
        },
        // localStorage存储
        setHistoryList(keyword){
          let flag = true;
          let tmpobj,tmpidx;
          this.historyList.forEach((item,idx)=>{
            if(keyword.trim() === item.value.trim()){//关键字相同，type不同-更新type
                item.type = this.type;
                flag = false;
                tmpobj = item;tmpidx=idx;
            }
          })
          //有重复关键字，把此关键字推到第一位
          !flag && this.historyList.splice(tmpidx,1) && this.historyList.unshift(tmpobj);
          //无重复关键字，直接存入localstorage
          flag  && this.historyList.unshift({type:this.type,value:keyword});
          this.historyList.length > 10 && (this.historyList = this.historyList.slice(0,10));
          localStorage.setItem('searchList', JSON.stringify(this.historyList));
        },
        // 获取参数
        getParams(name) {
          var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
          var r = window.location.search.substr(1).match(reg);
          if (r != null) return unescape(r[2]);
          return null;
        }
      }
    });
  };
  